/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FactoryPanel.java
 *
 * Created on Feb 27, 2012, 3:39:14 PM
 */
package core;

import java.awt.Dimension;
import javax.swing.ButtonModel;
import javax.swing.JFrame;
import javax.swing.JScrollPane;

/**
 *
 * @author user
 */
public class FactoryPanel extends javax.swing.JPanel {

    public static boolean run = false;
    Genetic thread;
    JFrame frame;
    
    int[] fittestIndividu;
    
    /** Creates new form FactoryPanel */
    public FactoryPanel(JFrame frame) {
        initComponents();
        run = false;        
        this.frame = frame;        
    }
    
    public void setGARun(boolean value) {
        run = value;
        if(value) {
            toggleStartButton.setText("stop");
        }else{
            toggleStartButton.setText("start");
        }
        System.out.println();
    }
    
    public boolean getGARun() {
        return run;
    }
    
    public void setFittestIndividu(int[] individu) {
        StringBuilder sb = new StringBuilder("");
        for(int a:individu) {
            sb.append(a+" ");
        }
        fittestIndividuLabel.setText(sb.toString());
        fittestIndividu = individu;
        fitnessLabel.setText(Genetic.FitnessFunction(individu)+"");
        simulateButton.setEnabled(true);
    }        
    
    public void setGeneration(int generation ) {
        generationLabel.setText(generation+"");
    }
    
    public int getMaxGeneration() {        
        int ret = Integer.parseInt(maxGenerationTextField.getText());        
        return ret;
    }
    
    public int getInitialPopulationNumber() {
        return Integer.parseInt(initialPopulationTextField.getText());
    }        

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        toggleStartButton = new javax.swing.JButton();
        initialPopulationTextField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        maxGenerationTextField = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        fittestIndividuLabel = new javax.swing.JLabel();
        generationLabel = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        fitnessLabel = new javax.swing.JLabel();
        simulateButton = new javax.swing.JButton();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jLabel5 = new javax.swing.JLabel();

        toggleStartButton.setText("Start");
        toggleStartButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toggleStartButtonActionPerformed(evt);
            }
        });

        initialPopulationTextField.setText("1");
        initialPopulationTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                initialPopulationTextFieldActionPerformed(evt);
            }
        });

        jLabel1.setText("Jumlah populasi awal : ");

        jLabel2.setText("Maksimum generasi : ");

        maxGenerationTextField.setText("1");
        maxGenerationTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                maxGenerationTextFieldActionPerformed(evt);
            }
        });

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);

        jLabel3.setText("Generasi : ");

        jLabel4.setText("Individu Terbaik : ");

        fittestIndividuLabel.setText("xxxx");

        generationLabel.setText("x");

        jLabel7.setText("Fitness : ");

        fitnessLabel.setText("xx");

        simulateButton.setText("Simulate!");
        simulateButton.setEnabled(false);
        simulateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                simulateButtonActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setText("BFS");
        jRadioButton1.setActionCommand("3");
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setSelected(true);
        jRadioButton2.setText("A*");
        jRadioButton2.setActionCommand("1");

        buttonGroup1.add(jRadioButton3);
        jRadioButton3.setText("Greedy Best First Search");
        jRadioButton3.setActionCommand("2");
        jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton3ActionPerformed(evt);
            }
        });

        jLabel5.setText("Path Finding : ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(initialPopulationTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(maxGenerationTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(toggleStartButton)
                        .addGap(28, 28, 28)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addComponent(fittestIndividuLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(generationLabel)
                        .addGap(28, 28, 28))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(fitnessLabel))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jRadioButton2)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jRadioButton1))
                    .addComponent(simulateButton))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(initialPopulationTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(maxGenerationTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(8, 8, 8)
                        .addComponent(toggleStartButton))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(generationLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(fittestIndividuLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(fitnessLabel))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jRadioButton2)
                                    .addComponent(jRadioButton3)
                                    .addComponent(jRadioButton1)))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel5)))))
                .addGap(18, 18, 18)
                .addComponent(simulateButton)
                .addGap(70, 70, 70))
        );

        fitnessLabel.getAccessibleContext().setAccessibleName("jLabel8");
    }// </editor-fold>//GEN-END:initComponents

private void toggleStartButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_toggleStartButtonActionPerformed
    if(!run) {        
        run = true;        
        thread = new Genetic(this);
        thread.start();
        toggleStartButton.setText("stop");
    }else{
        run = false;        
        //gak di settexdt start soalnya mungkin thread nya masih belom selesai 
    }
}//GEN-LAST:event_toggleStartButtonActionPerformed

private void initialPopulationTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_initialPopulationTextFieldActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_initialPopulationTextFieldActionPerformed

private void maxGenerationTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_maxGenerationTextFieldActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_maxGenerationTextFieldActionPerformed

private void simulateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_simulateButtonActionPerformed
    ButtonModel b = buttonGroup1.getSelection();            
    Board boardPanel = new Board(fittestIndividu, Integer.parseInt(b.getActionCommand()));
    if((Map.getMapWidth()*50 < 1000) && (Map.getMapHeight()*50<700)) {
        frame.add(boardPanel);
        frame.setSize(Map.getMapWidth()*50+20,Map.getMapHeight()*50+40);
        //frame.setPreferredSize(new Dimension(Map.getMapWidth()*50+20,Map.getMapHeight()*50 + 100));
        //frame.setPreferredSize(Map.get);
    }else{
        boardPanel.setPreferredSize(new Dimension(Map.getMapWidth()*50,Map.getMapHeight()*50));
        JScrollPane editorScroll = new JScrollPane(boardPanel);
        frame.getContentPane().add(editorScroll);      
        frame.setPreferredSize(new Dimension(300, 300));  
        frame.setSize(1000,700);     
        frame.setVisible(true);
        boardPanel.setVisible(true);
    }            
    frame.validate();    
    this.getParent().remove(this);    
}//GEN-LAST:event_simulateButtonActionPerformed

private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jRadioButton1ActionPerformed

private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton3ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jRadioButton3ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel fitnessLabel;
    private javax.swing.JLabel fittestIndividuLabel;
    private javax.swing.JLabel generationLabel;
    private javax.swing.JTextField initialPopulationTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField maxGenerationTextField;
    private javax.swing.JButton simulateButton;
    private javax.swing.JButton toggleStartButton;
    // End of variables declaration//GEN-END:variables
}
